package demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import demo.model.User;
import demo.service.LoginService;
import demo.utils.SysLogUtil;

@Controller
@RequestMapping("/home")
public class LoginController { 
	
	private LoginService loginService;

	@Autowired
	public void setLoginService(LoginService loginService)
	{
		this.loginService = loginService;
	}
	
	//处理路径：/login?id={id}
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public ModelAndView login(@RequestParam("id") Integer id){
		User user = loginService.getUser(id);
		ModelAndView mav = null;
		if (user != null) {
			mav = new ModelAndView("success");  
			mav.addObject(user);
		} else {
			mav = new ModelAndView("error");
		}
        SysLogUtil.print(mav.getViewName());
        return mav; 
	}
	
	//Restful路径: /login2/{id}
	@RequestMapping(value="/login/{id}", method=RequestMethod.GET)
	public ModelAndView login2(@PathVariable("id") Integer id){
		User user = loginService.getUser(id);
		ModelAndView mav = null;
		if (user != null) {
			mav = new ModelAndView("success");  
			mav.addObject(user);
		} else {
			mav = new ModelAndView("error");
		}
        
        SysLogUtil.print(mav.getViewName());
        return mav; 
	}
	
	//Restful路径: /login?username={id}&&password={password}
		@RequestMapping(value="/login", method=RequestMethod.POST)
		public ModelAndView login3(@RequestParam("username") String username, @RequestParam("password") String password){
			User user = loginService.getUserByName(username);
			ModelAndView mav = null;
			if (user != null) {
				mav = new ModelAndView("success");  
				mav.addObject(user);
			} else {
				mav = new ModelAndView("error");
			}
	        
	        SysLogUtil.print(mav.getViewName());
	        return mav; 
		}
}
